package com.wen.aop;

import com.wen.pojo.SysLog;
import com.wen.service.SysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * 日志类
 *
 * @author 20212
 */
@Component
@Aspect
public class LogAop {
    @Autowired
    private SysLogService sysLogService;
    @Autowired
    private HttpServletRequest request;
    /**
     * 开始时间
     */
    private Date visitTime;
    /**
     * 访问的类
     */
    private Class clazz;
    /**
     * 访问的方法
     */
    private Method method;

    /**
     * 前置通知
     * 主要获取开始时间，执行的类是哪一个
     * 执行的是哪一个方法
     *
     * @param jp
     */
    @Before("execution(* com.wen.controller.*.*(..))")
    public void doBefore(JoinPoint jp) throws NoSuchMethodException {
        System.out.println("=>" + "====================================");
        //当前时间就是开始时间
        visitTime = new Date();
        //访问的类
        clazz = jp.getTarget().getClass();
        //访问的方法名字
        String methodName = jp.getSignature().getName();
        Object[] args = jp.getArgs();
        //获取具体执行的方法的Method都西昂
        if (args == null || args.length == 0) {
            method = clazz.getMethod(methodName);
        } else {
            Class[] classArgs = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                classArgs[i] = args[i].getClass();
            }
            method = clazz.getMethod(methodName, classArgs);
        }


    }

    /**
     * 后置通知
     *
     * @param jp
     */
    @After("execution(* com.wen.controller.*.*(..))")
    public void doAfter(JoinPoint jp) throws Exception {
        //获取访问的市场
        long time = System.currentTimeMillis() - visitTime.getTime();
        String url = "";
        //获取url
        if (clazz != null && method != null && clazz != LogAop.class) {
            //1.获取类上的@RequsetMapping
            RequestMapping classAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            if (classAnnotation != null) {
                String[] classValue = classAnnotation.value();
                //2.获取方法上的@RequestMapping
                RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
                if (methodAnnotation != null) {
                    String[] methodValue = methodAnnotation.value();
                    url = classValue[0] + methodValue[0];
                }

            }
        }
        //获取访问的ip地址
        String ip = request.getRemoteAddr();
        ip = ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
        //获得当前操作的用户
        SecurityContext context = SecurityContextHolder.getContext();
        User principal = (User) context.getAuthentication().getPrincipal();
        String userName = principal.getUsername();
        SysLog syslog = new SysLog();
        syslog.setExecutionTime(time);
        syslog.setIp(ip);
        syslog.setMethod("[类名] " + clazz.getName() + "[方法名] " + method.getName());
        syslog.setUrl(url);
        syslog.setUserName(userName);
        syslog.setVisitTime(visitTime);
        sysLogService.add(syslog);
        System.out.println("=>" + syslog);
    }
}
